package OptimalAlgorithm.BinarySearch;
//给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
//eg:
//输入: nums = [1,3,5,6], target = 2
//输出: 1
public class SearchInsert {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right){
            int mid = left + (right - left) / 2;
            if(nums[mid] < target){
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        if(nums[left] < target){
            return nums.length;
        }
        return left;
    }
}
